package com.wry.wry_test.AQS;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Exchanger;

/**
 * @Author: wangrongyi
 * @Date: 2024/11/7 16:50
 * @Description:
 */
public class CyclicBarrierTest {

    private static final Runnable runnable = () -> System.out.println("收集到7颗龙珠,召唤神龙");

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, runnable);

        for (int i = 0; i < 7; i++) {
            final int temp = i + 1;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "\t收集到第" + temp + "颗龙珠");
                try {
                    int await = cyclicBarrier.await();
                    System.out.println("还剩几个:" + await);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }, "线程" + i).start();
        }



    }


}
